package com.autumn.mybatis.criterion;

import com.autumn.mybatis.criterion.expressions.ColumnExpression;
import com.autumn.mybatis.criterion.expressions.ConstantExpression;
import com.autumn.mybatis.criterion.expressions.AbstractExpression;
import com.autumn.mybatis.criterion.expressions.PropertyExpression;
import com.autumn.mybatis.metadata.EntityTable;
import com.autumn.util.ExceptionUtils;
import com.autumn.util.TypeUtils;

/**
 * 解析抽象
 * 
 * @author 老码农
 *
 *         2017-10-29 16:45:49
 */
abstract class AbstractParser {
	private final EntityTable entityTable;

	/**
	 * 无表实例化
	 */
	public AbstractParser() {
		this.entityTable = null;
	}

	/**
	 * 转入 entityClass 实例化
	 * 
	 * @param entityClass
	 *            实体类型
	 */
	public AbstractParser(Class<?> entityClass) {
		if (entityClass == null) {
			this.entityTable = null;
		} else {
			this.entityTable = EntityTable.getTable(entityClass);
		}
	}

	/**
	 * 转入 EntityTable 实例化
	 * 
	 * @param entityTable
	 *            实体表
	 */
	public AbstractParser(EntityTable entityTable) {
		this.entityTable = entityTable;
	}

	/**
	 * 获取表
	 * 
	 * @return
	 */
	public final EntityTable getEntityTable() {
		return entityTable;
	}

	/**
	 * 是否是实体表
	 * 
	 * @return
	 */
	public final boolean isEntityTable() {
		return this.getEntityTable() != null;
	}

	/**
	 * 列表达式
	 * 
	 * @param name
	 *            属性或列名称
	 * @return
	 */
	protected final ColumnExpression column(String name) {
		ExceptionUtils.checkNotNullOrBlank(name, "name");
		if (entityTable != null) {
			return AbstractExpression.property(this.entityTable, name);
		} else {
			return AbstractExpression.column(name);
		}
	}

	/**
	 * 常量表达式
	 * 
	 * @param column
	 *            列
	 * @param value
	 *            值
	 * @return
	 */
	protected final ConstantExpression constant(ColumnExpression column, Object value) {
		return AbstractExpression.constant(constantValue(column, value));
	}

	/**
	 * 常量值
	 * 
	 * @param column
	 *            列
	 * @param value
	 *            值
	 * @return
	 */
	protected final Object constantValue(ColumnExpression column, Object value) {
		if (column instanceof PropertyExpression) {
			PropertyExpression property = (PropertyExpression) column;
			return TypeUtils.toObjectConvert(property.getColumn().getJavaType(), value);
		}
		return value;
	}	
}
